package xiaobange.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xiaobange.common.R;
import xiaobange.entity.Category;
import xiaobange.service.CategoryService;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryCategory {

    @Autowired
    private CategoryService categoryService;

    /**
     * 菜品分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> CategoryPage(int page, int pageSize){
        //分页构造器
        Page<Category> categoryPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //排序条件
        lambdaQueryWrapper.orderByAsc(Category::getSort);
        //分页查询
        categoryService.page(categoryPage,lambdaQueryWrapper);
        return R.success(categoryPage);
    }

    /**
     * 添加菜品分类、套餐分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> addFoodClassify(@RequestBody Category category){
        //执行添加
        boolean save = categoryService.save(category);
        if (save){
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }
    }

    /**
     *      修改菜品、套餐
     * @param category
     * @return
     */
    @PutMapping
    public R<String> FoodUpdate(@RequestBody Category category){
        //执行修改操作
        boolean Update = categoryService.updateById(category);
        if (Update){
            return R.success("修改成功");
        }else {
            return R.error("修改失败");
        }
    }


    /**
     * 删除分类
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> DeleteByID(Long ids){
        categoryService.DeleteCheck(ids);
        return R.success("删除分类成功");
    }


    /**
     * 添加菜品下拉框获取数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> SelectList(Category category){
        //编写表达式
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //判断条件是否为空
        categoryLambdaQueryWrapper.eq(category.getType()!= null ,Category::getType,category.getType());
        //添加排序条件
        categoryLambdaQueryWrapper.orderByAsc(Category::getSort);
        List<Category> list = categoryService.list(categoryLambdaQueryWrapper);
        return R.success(list);
    }

}
